package com.ag.admin.web.tags;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.ag.admin.service.IAuthorizeService;

/**
 * 角色标签，即根据角色ID来显隐标签
 * 
 * @author Administrator
 * 
 */
public class RoleTag extends TagSupport implements ApplicationContextAware {
	private static final long serialVersionUID = -5082619765870671777L;

	private static ApplicationContext context;// 声明一个静态变量保存

	private IAuthorizeService authorizeService;

	private String roleID;

	public String getRoleID() {
		return roleID;
	}

	public void setRoleID(String roleID) {
		this.roleID = roleID;
	}

	@Override
	public int doStartTag() throws JspException {
		// authorizeService = (IAuthorizeService) context
		// .getBean("AuthorizeServiceImpl");
		// HttpSession session = pageContext.getSession();
		// if (authorizeService.checkCompetence(session, permission)) {
		// return EVAL_BODY_INCLUDE;
		// }
		// return SKIP_BODY;

		authorizeService = (IAuthorizeService) context
				.getBean("AuthorizeServiceImpl");
		HttpSession session = pageContext.getSession();
		if (authorizeService.checkRoleGen(session, roleID)) {
			return EVAL_BODY_INCLUDE;
		}
		return SKIP_BODY;
	}

	@Override
	public void setApplicationContext(ApplicationContext contex)
			throws BeansException {
		this.context = contex;
	}

	public static ApplicationContext getContext() {
		return context;
	}
}
